﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.Outstorage
{
    public partial class ReturnGoods : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    BindGV1(user_id);
                }


                
            }


        }

        //查询
        protected void btn_sel_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;
                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                BindGV1(user_id);
            }
        }
        //添加到退库清单
        protected void btn_addReturn_Click(object sender, EventArgs e)
        {
            string msg = CheckCboList();

            if (!string.IsNullOrEmpty(msg))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + msg + "');</script>");
            }
            else
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    BindGV2(user_id);

                    BindGV1(user_id);
                }
                else
                {
                    Maticsoft.Common.MessageBox.Show(this, "由于长时间未操作，登录信息已过期，请重新登陆!");
                }

            }
        }

        //绑定退库清单
        private void BindGV2(int user_id)
        {
            //获取选择的出库清单
            List<lcs.Model.returnGoods> li = GetCboList(user_id);

            //将前边页已选择的退库清单加入
            if (Session["returnGoods"] != null)
            {
                List<lcs.Model.returnGoods> li_session = Session["returnGoods"] as List<lcs.Model.returnGoods>;

                foreach (lcs.Model.returnGoods r in li_session)
                {
                    li.Add(r);
                }

                Session["returnGoods"] = li;
            }
            else
            {
                Session["returnGoods"] = li;
            }

            this.GridView2.DataSource = this.GetCboDataSet(li);
            this.GridView2.DataBind();
        }
        

        private void BindGV1(int user_id)
        {
            string projectName = this.txt_projectName.Text;
            string material = this.txt_materialinfo.Text;
            string strWhere = " cguid=" + user_id;
            if (!string.IsNullOrEmpty(projectName))
            {
                strWhere += " and projectName like '%" + projectName + "%' ";
            }
            if (!string.IsNullOrEmpty(material))
            {
                strWhere += " and material like '%" + material + "%' ";
            }

            DataSet ds = new lcs.BLL.inventory().GetListMaterialInventoryInstore(strWhere);

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;
                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                BindGV1(user_id);
            }
        }

        //验证退库数量与入库数量是否相等
        private string CheckCboList()
        {
            string msg = "";

            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox cbo = this.GridView1.Rows[i].FindControl("cbo_check") as CheckBox;

                if (cbo != null && cbo.Checked)
                {
                    TextBox txt_returnCount = this.GridView1.Rows[i].FindControl("txt_returnCount") as TextBox;//退库数量
                    Label lbl_instoreNums = this.GridView1.Rows[i].FindControl("lbl_instoreNums") as Label;//入库量
                    Label lbl_mid = this.GridView1.Rows[i].FindControl("lbl_mid") as Label;//物料ID

                    decimal return_count = 0;
                    decimal instore_count = 0;
                    int mid = 0;

                    try
                    {
                        if (!string.IsNullOrEmpty(txt_returnCount.Text))
                        {
                            return_count = Convert.ToDecimal(txt_returnCount.Text);
                        }
                        else
                        {
                            msg += @"\n 请填写退库数量！";
                        }
                    }
                    catch (Exception)
                    {

                        msg += @"\n 退库数量请填写数字！";
                    }

                    if (!string.IsNullOrEmpty(lbl_instoreNums.Text))
                    {
                        instore_count = Convert.ToDecimal(lbl_instoreNums.Text);
                    }
                    if (!string.IsNullOrEmpty(lbl_mid.Text))
                    {
                        mid = Convert.ToInt32(lbl_mid.Text);
                    }


                    if (return_count - instore_count > 0)
                    {
                        lcs.Model.materialinfo m = new lcs.BLL.materialinfo().GetModel(mid);

                        msg += @"\n “" + m.material + "”退库数量大于实际入库数量，请重新填写！";
                    }


                }
            }

            return msg;
        }

        private List<lcs.Model.returnGoods> GetCboList(int user_id)
        {
            List<lcs.Model.returnGoods> li = new List<Model.returnGoods>();

            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox cbo = this.GridView1.Rows[i].FindControl("cbo_check") as CheckBox;

                if (cbo != null && cbo.Checked)
                {
                    TextBox txt_returnCount = this.GridView1.Rows[i].FindControl("txt_returnCount") as TextBox;//退库数量
                   
                    Label lbl_mid = this.GridView1.Rows[i].FindControl("lbl_mid") as Label;//物料ID
                    Label lbl_inid = this.GridView1.Rows[i].FindControl("lbl_inid") as Label;//库存ID
                    Label lbl_material = this.GridView1.Rows[i].FindControl("lbl_material") as Label;//物料描述
                    Label lbl_unit = this.GridView1.Rows[i].FindControl("lbl_unit") as Label;//数量单位
                    Label lbl_proid = this.GridView1.Rows[i].FindControl("lbl_proid") as Label;//项目编号
                    Label lbl_projectname = this.GridView1.Rows[i].FindControl("lbl_projectname") as Label;//项目名称


                    lcs.Model.returnGoods r = new Model.returnGoods();

                    r.mid = int.Parse(lbl_mid.Text);
                    r.proid = lbl_proid.Text;
                    r.projectName = lbl_projectname.Text;

                    if (!string.IsNullOrEmpty(txt_returnCount.Text))
                    {
                        r.return_nums = Convert.ToDecimal(txt_returnCount.Text);
                    }
                    else
                    {
                        r.return_nums = 0;
                    }
                    if (!string.IsNullOrEmpty(this.txt_returnTime.Text))
                    {
                        r.returnTime = Convert.ToDateTime(txt_returnTime.Text);
                    }
                    else
                    {
                        r.returnTime = System.DateTime.Now;
                    }

                    r.unit = lbl_unit.Text;
                    r.user_id = user_id;
                    r.return_user_name = this.txt_returnName.Text;
                    r.material = lbl_material.Text;
                    r.inid = int.Parse(lbl_inid.Text);

                    li.Add(r);
                }
            }

            return li;
        }
        
        private DataTable GetCboDataSet(List<lcs.Model.returnGoods> li)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("returnCount", Type.GetType("System.String"));//退库数量
            dt.Columns.Add("material", Type.GetType("System.String"));//物料描述
            dt.Columns.Add("unit", Type.GetType("System.String"));//数量单位
            dt.Columns.Add("projectname", Type.GetType("System.String"));//项目名称
            dt.Columns.Add("proid", Type.GetType("System.String"));//项目编号
            dt.Columns.Add("mid", Type.GetType("System.String"));//物料ID

            foreach (lcs.Model.returnGoods r in li)
            {
                DataRow row = dt.NewRow();
                row["returnCount"] = r.return_nums.ToString();
                row["material"] = r.material;
                row["unit"] = r.unit;
                row["proid"] = r.proid;
                row["projectname"] = r.projectName;
                row["mid"] = r.mid.ToString();

                dt.Rows.Add(row);
            }

            return dt;
        }

        //提交出库单
        protected void btn_submit_Click(object sender, EventArgs e)
        {
            if (Session["returnGoods"] != null)
            {
                string returnNo = "rgTime"+System.DateTime.Now.ToString("yyyyMMddHHmmssffff");//出库单号

                List<lcs.Model.returnGoods> li = Session["returnGoods"] as List<lcs.Model.returnGoods>;


                lcs.BLL.returnGoods rm = new BLL.returnGoods();
                lcs.BLL.inventory im = new BLL.inventory();

                foreach (lcs.Model.returnGoods r in li)
                {
                    r.returnNo = returnNo;
                    r.remark = this.txt_remark.Text;
                    r.purposeProjectName = this.txt_purposeProjectName.Text;

                    lcs.Model.inventory i = im.GetModel(r.inid.Value);

                    i.nums = i.nums + r.return_nums;//库存数量加上退库数量

                    im.Update(i);

                    rm.Add(r);
                }

                Session.Remove("returnGoods");
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    BindGV2(user_id);
                    BindGV1(user_id);
                }

                //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('操作成功！您的出库清单号："+batchId+"');</script>");
                Maticsoft.Common.MessageBox.Show(this, "操作成功！您的退库清单号：" + returnNo);

            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "由于长时间未操作，信息已过期，请重新登陆!");
            }

        }

        protected void btn_re_Click(object sender, EventArgs e)
        {
            Session.Remove("returnGoods");
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                BindGV2(user_id);
                BindGV1(user_id);
            }

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }
        
    }
}